﻿using Dto;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;

namespace ZY8.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HotelController : ControllerBase
    {
        public HotelController(IHotelBLL bll)
        {
            Bll = bll;
        }

        public IHotelBLL Bll { get; }
        [HttpPost]
        public async Task<int> AddHotel(HotelModel hotel)
        {
            return await Bll.AddHotel(hotel);

        }
        [HttpGet]
        public HotelShowDto HotelShow(string? hid, int? tid, int pageindex, int pagesize)
        {
            return Bll.HotelShow(hid, tid, pageindex, pagesize);
        }
        [HttpGet]
        public IActionResult ExPort() 
        {
            //使用工具创建Excel对象
            XSSFWorkbook wb=new XSSFWorkbook();
            //在Excel对象中创建表
            var sheets=wb.CreateSheet("房间表");
            //在表中创建行
            var rows = sheets.CreateRow(0);
            //在行中创建单元格
            var cells=rows.CreateCell(0);
            //给单元格赋值
            cells.SetCellValue("你好,今天天气晴");



            //把Excel对象变成文件并且下载
            //先把Excel对象变成byte[]字节数组,再把字符数组变在文件下载
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream()) 
            {
                wb.Write(ms);
                bytes = ms.ToArray();
            }
            var mie = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes,mie,"今天很开心.xlsx");

        }
    }
}
